/**   
* @Title: PrivilegeGroupServiceImpl.java 
* @Package com.nebula.upms.service.impl 
* @Description: TODO(用一句话描述该文件做什么) 
* @author zhanghui   
* @date 2016年2月4日 下午3:33:57 
* @version V1.0   
*/
package com.nebula.upms.service.biz.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.fasterxml.jackson.core.type.TypeReference;
import com.nebula.common.datasource.mybatis.pager.DataPager;
import com.nebula.common.datasource.mybatis.pager.Pager;
import com.nebula.common.util.UUIDGenerator;
import com.nebula.upms.service.biz.PrivilegeGroupService;
import com.nebula.upms.service.model.po.*;
import com.nebula.upms.service.model.vo.PrivilegeVO;
import com.nebula.upms.service.support.constant.UpmsLocalFinal;
import com.nebula.upms.service.support.datasource.UpmsDs;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.nebula.common.framework.BaseService;


/** 
* @ClassName: PrivilegeGroupServiceImpl 
* @Description: TODO(这里用一句话描述这个类的作用) 
* @author zhanghui
* @date 2016年2月4日 下午3:33:57 
*  
*/
@Service("privilegeGroupService")
@Transactional
public class PrivilegeGroupServiceImpl extends BaseService implements PrivilegeGroupService {

	/*
	 * <p>Title: getUpmsPrivilegeGroupPOListForPager</p>
	 * <p>Description: </p>
	 * @param params
	 * @param pager
	 * @return
	 * @see com.nebula.upms.service.PrivilegeGroupService#getUpmsPrivilegeGroupPOListForPager(java.util.Map, com.nebula.common.plugs.pager.Pager)
	 */
	@Override
	public DataPager<UpmsPrivilegeGroupPO> getUpmsPrivilegeGroupPOListForPager(Map<String, Object> params, Pager pager) {
		UpmsPrivilegeGroupPOExample example=new UpmsPrivilegeGroupPOExample();
		UpmsPrivilegeGroupPOExample.Criteria c=example.createCriteria();
		if(null!=params.get("privilegeGroupName")){
			c.andNameLike("%"+params.get("privilegeGroupName").toString()+"%");
		}
		
		example.setOrderByClause(" priority asc ");
		DataPager<UpmsPrivilegeGroupPO> dp= UpmsDs.instance().mybatis().ds().mapper().selectDataPager(example,pager);
		return dp;
	}

	/*
	 * <p>Title: getFirstUpmsPrivilegeGroupPO</p>
	 * <p>Description: </p>
	 * @return
	 * @see com.nebula.upms.service.PrivilegeGroupService#getFirstUpmsPrivilegeGroupPO()
	 */
	@Override
	public UpmsPrivilegeGroupPO getFirstUpmsPrivilegeGroupPO() {
		UpmsPrivilegeGroupPOExample example=new UpmsPrivilegeGroupPOExample();
		example.setOrderByClause(" priority asc ");
		UpmsPrivilegeGroupPO po=UpmsDs.instance().mybatis().ds().mapper().selectFirstByExample(example);
		return po;
	}

	/*
	 * <p>Title: savePrivilegeGroup</p>
	 * <p>Description: </p>
	 * @param paramsData
	 * @see com.nebula.upms.service.PrivilegeGroupService#savePrivilegeGroup(java.lang.String)
	 */
	@Override
	public void savePrivilegeGroup(UpmsPrivilegeGroupPO UpmsPrivilegeGroupPO,Map<String,Object> params) {
		UpmsPrivilegeGroupPO.setPrivilegeGroupId(UUIDGenerator.random32UUID());
		UpmsPrivilegeGroupPO.setType(UpmsLocalFinal.PRIVILEGE_GROUP_TYPE_Y21);
		UpmsPrivilegeGroupPO.setCreateTime(new Date().getTime());
		UpmsPrivilegeGroupPOExample UpmsPrivilegeGroupPOExample=new UpmsPrivilegeGroupPOExample();
		UpmsPrivilegeGroupPOExample.setOrderByClause(" PRIORITY ASC");
		UpmsPrivilegeGroupPO last=UpmsDs.instance().mybatis().ds().mapper().selectFirstByExample(UpmsPrivilegeGroupPOExample);
		int priority=0;
		if(null!=last){
			priority=last.getPriority()+1;
		}
		UpmsPrivilegeGroupPO.setPriority(priority);
		UpmsDs.instance().mybatis().ds().mapper().insert(UpmsPrivilegeGroupPO);
		
		String paramsData=params.get("paramsData").toString();
		ObjectMapper mapper = new ObjectMapper();
		
		try {
			
			/**
			List<LinkedHashMap> privilegeVOList=mapper.readValue(paramsData, List.class);
			for (LinkedHashMap linkedHashMap : privilegeVOList) {
				String privilegeId=linkedHashMap.get("privilegeId").toString();
				List operateList=(List)linkedHashMap.get("operateList");
				for (Object operateObj : operateList) {
					LinkedHashMap hm=(LinkedHashMap)operateList;
					System.out.println(hm.get("operateId"));
				}
			}
			**/
			
			
			List<PrivilegeVO> privilegeVOList=mapper.readValue(paramsData, new TypeReference<List<PrivilegeVO>>() {});
			UpmsPrivilegePOExample UpmsPrivilegePOExample=new UpmsPrivilegePOExample();
			List<UpmsPrivilegePO> allPrivilegeList=UpmsDs.instance().mybatis().ds().mapper().selectByExample(UpmsPrivilegePOExample);
			
			List<PrivilegeVO> fullPrivilegeVOList=new ArrayList<>();
			this.buildFullPrivilege(privilegeVOList, allPrivilegeList,fullPrivilegeVOList);
			//privilegeVOList.addAll(fullPrivilegeVOList);
			
			for (PrivilegeVO privilegeVO : fullPrivilegeVOList) {
				//System.out.println(privilegeVO.getPrivilegeId());
			}
			
			
			for (PrivilegeVO privilegeVO : fullPrivilegeVOList) {
				UpmsPrivilegeGroupPrivilegePOKey UpmsPrivilegeGroupPrivilegePOKey=new UpmsPrivilegeGroupPrivilegePOKey();
				UpmsPrivilegeGroupPrivilegePOKey.setPrivilegeGroupId(UpmsPrivilegeGroupPO.getPrivilegeGroupId());
				UpmsPrivilegeGroupPrivilegePOKey.setPrivilegeId(privilegeVO.getPrivilegeId());
				UpmsDs.instance().mybatis().ds().mapper().insert(UpmsPrivilegeGroupPrivilegePOKey);
				List<UpmsOperatePO> UpmsOperatePOList=privilegeVO.getOperateList();
				if(null!=UpmsOperatePOList){
					for (UpmsOperatePO UpmsOperatePO : UpmsOperatePOList) {
						UpmsPrivilegeGroupPrivilegeOperatePOKey upmsPrivilegeGroupPrivilegeOperatePO=
								new UpmsPrivilegeGroupPrivilegeOperatePOKey();
						upmsPrivilegeGroupPrivilegeOperatePO.setOperateId(UpmsOperatePO.getOperateId());
						upmsPrivilegeGroupPrivilegeOperatePO.setPrivilegeGroupId(UpmsPrivilegeGroupPO.getPrivilegeGroupId());
						upmsPrivilegeGroupPrivilegeOperatePO.setPrivilegeId(privilegeVO.getPrivilegeId());
						UpmsDs.instance().mybatis().ds().mapper().insert(upmsPrivilegeGroupPrivilegeOperatePO);
					}
				}
				
			}
			
			
			
		} catch (Exception e) {
			e.printStackTrace();
		} 
		
	}
	
	
	private void initFullPrivilege(UpmsPrivilegePO thisPO,List<UpmsPrivilegePO> allPrivilegeList,List<PrivilegeVO> fullPrivilegeVOList){
		for (UpmsPrivilegePO upmsPrivilegePO : allPrivilegeList) {
			if(upmsPrivilegePO.getPrivilegeId().equals(thisPO.getPrivilegePid())){
				PrivilegeVO vo=new PrivilegeVO();
				vo.setPrivilegeId(upmsPrivilegePO.getPrivilegeId());
				
				
				boolean isExit=false;
				for(PrivilegeVO tempVO : fullPrivilegeVOList){
					if(tempVO.getPrivilegeId().equals(upmsPrivilegePO.getPrivilegeId())){
						isExit=true;
						break;
					}
				}
				if(!isExit){
					fullPrivilegeVOList.add(vo);
					this.initFullPrivilege(upmsPrivilegePO, allPrivilegeList, fullPrivilegeVOList);
				}
			}
		}
	}
	
	private void buildFullPrivilege(List<PrivilegeVO> privilegeVOList,List<UpmsPrivilegePO> allPrivilegeList,List<PrivilegeVO> fullPrivilegeVOList){
		for (PrivilegeVO privilegeVO : privilegeVOList) {
			UpmsPrivilegePO thisPO=null;
			for (UpmsPrivilegePO UpmsPrivilegePO : allPrivilegeList) {
				if(privilegeVO.getPrivilegeId().equals(UpmsPrivilegePO.getPrivilegeId())){
					thisPO=UpmsPrivilegePO;
					break;
				}
			}
			fullPrivilegeVOList.add(privilegeVO);
			this.initFullPrivilege(thisPO, allPrivilegeList, fullPrivilegeVOList);
		}
		
	}

	/*
	 * <p>Title: getUpmsPrivilegeGroupPOByKey</p>
	 * <p>Description: </p>
	 * @param privilegeGroupId
	 * @return
	 * @see com.nebula.upms.service.PrivilegeGroupService#getUpmsPrivilegeGroupPOByKey(java.lang.String)
	 */
	@Override
	public UpmsPrivilegeGroupPO getUpmsPrivilegeGroupPOByKey(String privilegeGroupId) {
		UpmsPrivilegeGroupPO UpmsPrivilegeGroupPO=UpmsDs.instance().mybatis().ds().mapper().selectByPrimaryKey(UpmsPrivilegeGroupPO.class, privilegeGroupId);
		return UpmsPrivilegeGroupPO;
	}

	/*
	 * <p>Title: updatePrivilegeGroup</p>
	 * <p>Description: </p>
	 * @param UpmsPrivilegeGroupPO
	 * @param params
	 * @see com.nebula.upms.service.PrivilegeGroupService#updatePrivilegeGroup(com.hbdiy.common.model.po.UpmsPrivilegeGroupPO, java.util.Map)
	 */
	@Override
	public void updatePrivilegeGroup(UpmsPrivilegeGroupPO UpmsPrivilegeGroupPO, Map<String, Object> params) {
		UpmsDs.instance().mybatis().ds().mapper().updateByPrimaryKeySelective(UpmsPrivilegeGroupPO);
		String paramsData=params.get("paramsData").toString();
		ObjectMapper mapper = new ObjectMapper();
		try {
			UpmsPrivilegeGroupPrivilegeOperatePOExample example1=new UpmsPrivilegeGroupPrivilegeOperatePOExample();
			example1.createCriteria().andPrivilegeGroupIdEqualTo(UpmsPrivilegeGroupPO.getPrivilegeGroupId());
			UpmsDs.instance().mybatis().ds().mapper().deleteByExample(example1);
			
			UpmsPrivilegeGroupPrivilegePOExample example2=new UpmsPrivilegeGroupPrivilegePOExample();
			example2.createCriteria().andPrivilegeGroupIdEqualTo(UpmsPrivilegeGroupPO.getPrivilegeGroupId());
			UpmsDs.instance().mybatis().ds().mapper().deleteByExample(example2);
			
			
			
			List<PrivilegeVO> privilegeVOList=mapper.readValue(paramsData, new TypeReference<List<PrivilegeVO>>() {});
			UpmsPrivilegePOExample UpmsPrivilegePOExample=new UpmsPrivilegePOExample();
			List<UpmsPrivilegePO> allPrivilegeList=UpmsDs.instance().mybatis().ds().mapper().selectByExample(UpmsPrivilegePOExample);
			
			List<PrivilegeVO> fullPrivilegeVOList=new ArrayList<>();
			this.buildFullPrivilege(privilegeVOList, allPrivilegeList,fullPrivilegeVOList);
			
			
			for (PrivilegeVO privilegeVO : fullPrivilegeVOList) {
				UpmsPrivilegeGroupPrivilegePOKey UpmsPrivilegeGroupPrivilegePOKey=new UpmsPrivilegeGroupPrivilegePOKey();
				UpmsPrivilegeGroupPrivilegePOKey.setPrivilegeGroupId(UpmsPrivilegeGroupPO.getPrivilegeGroupId());
				UpmsPrivilegeGroupPrivilegePOKey.setPrivilegeId(privilegeVO.getPrivilegeId());
				UpmsDs.instance().mybatis().ds().mapper().insert(UpmsPrivilegeGroupPrivilegePOKey);
				List<UpmsOperatePO> UpmsOperatePOList=privilegeVO.getOperateList();
				if(null!=UpmsOperatePOList){
					for (UpmsOperatePO UpmsOperatePO : UpmsOperatePOList) {
						UpmsPrivilegeGroupPrivilegeOperatePOKey upmsPrivilegeGroupPrivilegeOperatePO=
								new UpmsPrivilegeGroupPrivilegeOperatePOKey();
						upmsPrivilegeGroupPrivilegeOperatePO.setOperateId(UpmsOperatePO.getOperateId());
						upmsPrivilegeGroupPrivilegeOperatePO.setPrivilegeGroupId(UpmsPrivilegeGroupPO.getPrivilegeGroupId());
						upmsPrivilegeGroupPrivilegeOperatePO.setPrivilegeId(privilegeVO.getPrivilegeId());
						UpmsDs.instance().mybatis().ds().mapper().insert(upmsPrivilegeGroupPrivilegeOperatePO);
					}
				}
				
			}
		} catch (Exception e) {
			e.printStackTrace();
		} 
		
		
	}

	/*
	 * <p>Title: selectStaffPrivilegeGroupPOList</p>
	 * <p>Description: </p>
	 * @param staffId
	 * @return
	 * @see com.nebula.upms.service.PrivilegeGroupService#selectStaffPrivilegeGroupPOList(java.lang.String)
	 */
	@Override
	public List<UpmsPrivilegeGroupPO> selectStaffPrivilegeGroupPOList(String staffId) {
		Map<String,Object> params=new HashMap<>();
		params.put("staffId", staffId);
		List<UpmsPrivilegeGroupPO> ls=UpmsDs.instance().mybatis().ds().mapper("PrivilegeMapper").selectList("selectStaffPrivilegeGroupPOList", params);
		return ls;
	}

	/**
	* Description:
	* @param privilegeGroupId
	* @return
	**/
	@Override
	public boolean removePrivilegeGroup(String privilegeGroupId) {
		UpmsPrivilegeGroupStaffPOExample example=new UpmsPrivilegeGroupStaffPOExample();
		example.createCriteria().andPrivilegeGroupIdEqualTo(privilegeGroupId);
		int count=(int)UpmsDs.instance().mybatis().ds().mapper().countByExample(example);
		if(count==0){
			UpmsPrivilegeGroupPrivilegePOExample example2=new UpmsPrivilegeGroupPrivilegePOExample();
			example2.createCriteria().andPrivilegeGroupIdEqualTo(privilegeGroupId);
			UpmsDs.instance().mybatis().ds().mapper().deleteByExample(example2);
			UpmsDs.instance().mybatis().ds().mapper().deleteByPrimaryKey(UpmsPrivilegeGroupPO.class, privilegeGroupId);
			return true;
		}
		
		return false;
	}

	@Override
	public List<UpmsPrivilegeGroupPO> getUpmsPrivilegeGroupPOList() {
		UpmsPrivilegeGroupPOExample example=new UpmsPrivilegeGroupPOExample();
		example.setOrderByClause("CREATE_TIME DESC");
		List<UpmsPrivilegeGroupPO> list=UpmsDs.instance().mybatis().ds().mapper().selectByExample(example);
		
		return list;
	}



	
}
